<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item :to="{ path: '/market/marketingPlay' }"> <i class="el-icon-lx-cascades"></i> 营销玩法 </el-breadcrumb-item>
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 兑换码 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <el-button type="primary" class="handle-del mr10" @click="addDialog">新增兑换码</el-button>
            <div class="handle-box">
                <el-input v-model="query.keyword" placeholder="输入兑换码名称" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>
            <el-table :data="tableData" class="table" ref="multipleTable" header-cell-class-name="table-header">
                <el-table-column prop="title" label="兑换码名称"></el-table-column>
                <el-table-column prop="num" label="兑换权益数"></el-table-column>
                <el-table-column label="兑换码状态">
                    <template slot-scope="scope">
                        <span :style="scope.row.time_status == 1 ? 'color: green' : 'color: #888'">
                            {{ scope.row.time_status == 1 ? '进行中' : '已失效' }}
                        </span>
                    </template>
                </el-table-column>
                <el-table-column label="有效期">
                    <template slot-scope="scope">
                        <span class="valide_date">起：{{ scope.row.start_time }}</span>
                        <span class="valide_date">止：{{ scope.row.end_time }}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="use_num" label="兑换限制"></el-table-column>
                <el-table-column prop="yl_num" label="兑换人数/个数"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-row>
                            <el-button
                                @click="deleteRow(scope.row.id, scope.$index)"
                                v-if="scope.row.time_status == 2"
                                type="danger"
                                size="mini"
                                >删除</el-button
                            >
                            <div v-else>
                                <el-button @click="exchangeCodeList(scope.row.id)" type="primary" size="mini">码库</el-button>
                                <el-button @click="edit(scope.row)" type="success" size="mini">编辑</el-button>
                                <el-button @click="end(scope.row.id)" type="warning" size="mini">使失效</el-button>
                            </div>
                        </el-row>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.page"
                    :page-size="query.rows"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>
        <!-- 新增兑换码弹出框 -->
        <el-dialog title="新增/编辑" :visible.sync="showDialog" width="30%">
            <el-form ref="form" :model="form" label-width="100px">
                <el-form-item label="兑换码名称">
                    <el-input v-model="form.title"></el-input>
                </el-form-item>
                <el-form-item label="库存">
                    <el-input v-model="form.num"></el-input>
                </el-form-item>
                <el-form-item label="活动时间">
                    <el-date-picker
                        v-model="useTimeDate"
                        type="datetimerange"
                        range-separator="至"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        value-format="yyyy-MM-dd HH:mm:ss"
                    >
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="每人可用">
                    <el-radio-group v-model="use_status">
                        <el-radio :label="true">不限次数</el-radio>
                        <el-radio :label="false">
                            <el-select style="width: 100px; margin-right: 10px" v-model="form.use_num" clearable>
                                <el-option v-for="item in options" :key="item" :value="item"></el-option> </el-select
                            >个码
                        </el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="兑换优惠券">
                    <el-button @click="chooseCourse" type="primary" size="small">添加优惠券</el-button>
                    <el-table
                        :data="selectRowData"
                        v-if="selectRowData.length !== 0"
                        class="table"
                        ref="multipleTable"
                        header-cell-class-name="table-header"
                    >
                        <el-table-column prop="name" label="优惠券名称"></el-table-column>
                        <el-table-column label="类型">
                            <template slot-scope="scope">
                                <span class="red">{{ scope.row.menkan_price > 0 ? '	满减卷' : '无门槛卷' }}</span>
                            </template>
                        </el-table-column>
                        <el-table-column label="优惠内容">
                            <template slot-scope="scope">
                                {{
                                    scope.row.menkan_price > 0
                                        ? '满' + scope.row.menkan_price + '减' + scope.row.jian_price
                                        : '减' + scope.row.jian_price
                                }}
                            </template>
                        </el-table-column>
                    </el-table>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="cancel">取 消</el-button>
                <el-button type="primary" @click="save">确 定</el-button>
            </span>
        </el-dialog>
        <!-- 打开优惠券弹框 -->
        <el-dialog width="36%" title="添加优惠券" @close="exit" :visible.sync="innerVisible" append-to-body>
            <el-form ref="form" :model="form" label-width="100px">
                <el-form-item>
                    <el-input v-model="query.keyword" placeholder="输入优惠券名称" class="handle-input mr10"></el-input>
                    <el-button type="primary" icon="el-icon-search" @click="courseSearch">搜索</el-button>
                </el-form-item>
                <el-table
                    :data="chooseCourseTableData"
                    @row-click="chooseRow"
                    class="chooseCourseTable"
                    ref="multipleTable"
                    header-cell-class-name="table-header"
                >
                    <el-table-column prop="name" label="优惠券名称"></el-table-column>
                    <el-table-column label="类型">
                        <template slot-scope="scope">
                            <span class="red">{{ scope.row.menkan_price > 0 ? '	满减卷' : '无门槛卷' }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="优惠内容">
                        <template slot-scope="scope">
                            {{
                                scope.row.menkan_price > 0
                                    ? '满' + scope.row.menkan_price + '减' + scope.row.jian_price
                                    : '减' + scope.row.jian_price
                            }}
                        </template>
                    </el-table-column>
                </el-table>
                <div class="pagination">
                    <el-pagination
                        background
                        layout="total, prev, pager, next"
                        :current-page="query.page"
                        :page-size="query.rows"
                        :total="courseListTotal"
                        @current-change="coursePageChange"
                    ></el-pagination>
                </div>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="exit">关 闭</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { exchangeCodeListData, couponListData, addExchangeCodeData, delExchangeCodeData } from '../../api/index';
export default {
    // name: 'exchangeCode',
    data() {
        return {
            query: {
                page: 1,
                rows: 4,
                keyword: ''
            },
            form: {
                info_id: '',
                title: '',
                num: '',
                start_time: '',
                end_time: '',
                use_num: ''
            },
            use_status: '',
            useTimeDate: [],
            options: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
            tableData: [],
            chooseCourseTableData: [],
            selectRowData: [],
            showDialog: false,
            innerVisible: false,
            pageTotal: 0,
            courseListTotal: 0
        };
    },
    created() {
        this.getData();
    },
    methods: {
        getData() {
            exchangeCodeListData(this.query).then((res) => {
                this.tableData = res.data.list;
                this.pageTotal = res.data.count;
            });
        },
        // 搜索
        handleSearch() {
            this.$set(this.query);
            this.getData();
        },
        // 课程搜索
        courseSearch() {
            this.chooseCourse();
        },
        // 分页
        handlePageChange(val) {
            this.$set(this.query, 'page', val);
            this.getData();
        },
        // 课程分页
        coursePageChange(val) {
            this.$set(this.query, 'page', val);
            this.chooseCourse();
        },
        //新增
        addDialog() {
            this.showDialog = true;
            this.selectRowData = [];
        },
        chooseCourse() {
            this.innerVisible = true;
            couponListData(this.query).then((res) => {
                this.chooseCourseTableData = res.data.list;
                this.courseListTotal = res.data.count;
            });
        },
        chooseRow(row) {
            this.selectRowData = [];
            this.selectRowData.push(row);
            this.form.info_id = row.id;
            this.innerVisible = false;
        },
        // 确定
        save() {
            this.form.start_time = this.useTimeDate[0];
            this.form.end_time = this.useTimeDate[1];
            if(this.use_status){
               this.form.use_num = 0;
            }
            addExchangeCodeData(this.form).then((res) => {
                if (res.code == 1) {
                    this.showDialog = false;
                    this.$message.success('操作成功');
                    this.getData()
                }
            });
        },
        // 码库
        exchangeCodeList(id) {
            this.$router.push({ path: '/exchangeCodeList', query: { id } });
        },
        //编辑
        edit(row) {
            this.selectRowData = [];
            this.showDialog = true;
            this.form = row;
            this.selectRowData.push(row.info[0]);
            this.useTimeDate = [row.start_time, row.end_time];
            
            if(row.use_num == 0) {
                this.use_status = true;
                this.form.use_num = 0;
            } else {
                this.use_status = false;
            }
            
        },
        //使失效
        end(id) {
           this.$confirm('确定使失效吗？', '提示', {
                type: 'warning'
            }).then(() => {
                addExchangeCodeData({ id: id, time_status: 2 }).then((res) => {
                    if (res.code == 1) {
                        this.$message.success('使失效成功');
                        this.getData();
                    }
                });
            }); 
        },
        //删除
        deleteRow(id, index) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            }).then(() => {
                delExchangeCodeData({ id: id }).then((res) => {
                    if (res.code == 1) {
                        this.$message.success('删除成功');
                        this.tableData.splice(index, 1);
                        this.getData();
                    }
                });
            });
        },
        //取消
        cancel() {
            this.showDialog = false;
            this.form = {};
            this.useTimeDate = '';
            this.use_status = '';
        },
        //关闭
        exit() {
            this.innerVisible = false;
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
    float: right;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.mr10 {
    margin-right: 10px;
}
.valide_date {
    float: left;
}
.red {
    color: #ea644a;
}
.chooseCourseTable {
    cursor: pointer;
}
</style>


